#include "UI_task.h"
#include "Data_MATH.h"
#include "bsp_adc.h"

UI_LCD_t UI_LCD;

static void fun_0();
static void fun_a1();
static void fun_b1();
static void fun_c1();
static void fun_d1();
static void fun_a21();
static void fun_a22();
static void fun_b21();
static void fun_b22();
static void fun_b23();
static void fun_b24();
static void fun_c21();
static void fun_d21();
static void fun_a31();
static void fun_a32();
static void fun_b31();
static void fun_b32();
static void fun_b33();
static void fun_b34();
static void fun_c31();
static void fun_d31();

void UI_task_init()
{
    BSP_Key_Init(&UI_LCD.Button[0]);
    BSP_Key_Init(&UI_LCD.Button[1]);
    BSP_Key_Init(&UI_LCD.Button[2]);
    BSP_Key_Init(&UI_LCD.Button[3]);

    UI_LCD.table[0] = (KEY_TABLE){0, 0, 1, 0, 0, (fun_0)};
    UI_LCD.table[1] = (KEY_TABLE){1, 4, 5, 2, 0, (fun_a1)};
    UI_LCD.table[2] = (KEY_TABLE){2, 1, 7, 3, 0, (fun_b1)};
    UI_LCD.table[3] = (KEY_TABLE){3, 2, 11, 4, 0, (fun_c1)};
    UI_LCD.table[4] = (KEY_TABLE){4, 3, 12, 1, 0, (fun_d1)};
    UI_LCD.table[5] = (KEY_TABLE){5, 6, 13, 6, 1, (fun_a21)};
    UI_LCD.table[6] = (KEY_TABLE){6, 5, 14, 5, 1, (fun_a22)};
    UI_LCD.table[7] = (KEY_TABLE){7, 10, 15, 8, 2, (fun_b21)};
    UI_LCD.table[8] = (KEY_TABLE){8, 7, 16, 9, 2, (fun_b22)};
    UI_LCD.table[9] = (KEY_TABLE){9, 8, 17, 10, 2, (fun_b23)};
    UI_LCD.table[10] = (KEY_TABLE){10, 9, 18, 7, 2, (fun_b24)};
    UI_LCD.table[11] = (KEY_TABLE){11, 11, 19, 21, 3, (fun_c21)};
    UI_LCD.table[12] = (KEY_TABLE){12, 12, 20, 12, 4, (fun_d21)};
    UI_LCD.table[13] = (KEY_TABLE){13, 13, 13, 13, 5, (fun_a31)};
    UI_LCD.table[14] = (KEY_TABLE){14, 14, 14, 14, 6, (fun_a32)};
    UI_LCD.table[15] = (KEY_TABLE){15, 15, 15, 15, 7, (fun_b31)};
    UI_LCD.table[16] = (KEY_TABLE){16, 16, 16, 16, 8, (fun_b32)};
    UI_LCD.table[17] = (KEY_TABLE){17, 17, 17, 17, 9, (fun_b33)};
    UI_LCD.table[18] = (KEY_TABLE){18, 18, 18, 18, 10, (fun_b34)};
    UI_LCD.table[19] = (KEY_TABLE){19, 19, 19, 19, 11, (fun_c31)};
    UI_LCD.table[20] = (KEY_TABLE){20, 20, 20, 20, 12, (fun_d31)};
}

void UI_task_loop()
{
    if(UI_LCD.Button[0].ButtonFlag == Flag_RUN)
    {
        Key_Loop(&UI_LCD.Button[0], UI_LCD.Menu_Parameter.key_value, UI_LCD.table[UI_LCD.Menu_Parameter.key_value].ok);
    }
    if(UI_LCD.Button[1].ButtonFlag == Flag_RUN)
    {
        Key_Loop(&UI_LCD.Button[1], UI_LCD.Menu_Parameter.key_value,
                 UI_LCD.table[UI_LCD.Menu_Parameter.key_value].go_back);
    }
    if(UI_LCD.Button[2].ButtonFlag == Flag_RUN)
    {
        Key_Loop(&UI_LCD.Button[2], UI_LCD.Menu_Parameter.key_value,
                 UI_LCD.table[UI_LCD.Menu_Parameter.key_value].left);
    }
    if(UI_LCD.Button[3].ButtonFlag == Flag_RUN)
    {
        Key_Loop(&UI_LCD.Button[3], UI_LCD.Menu_Parameter.key_value,
                 UI_LCD.table[UI_LCD.Menu_Parameter.key_value].right);
    }

    Menu_loop(&UI_LCD.Menu_Parameter, UI_LCD.table);
}

void USER_Key1_Callback()
{
    UI_LCD.Button[0].ButtonFlag = Flag_RUN;
}

void USER_Key2_Callback()
{
    UI_LCD.Button[1].ButtonFlag = Flag_RUN;
}

void USER_Key3_Callback()
{
    UI_LCD.Button[2].ButtonFlag = Flag_RUN;
}

void USER_Key4_Callback()
{
    UI_LCD.Button[3].ButtonFlag = Flag_RUN;
}

static void fun_0()
{
    ADC1data_t ADC1data = BSP_ADC1_Read();
    ADC2data_t ADC2data = BSP_ADC2_Read();

    LCD_ShowString(10, 5, (uint8_t*)"[READY]", GREEN, BLACK, 32, 0);

    LCD_ShowString(130, 5, (uint8_t*)"Vbus:", WHITE, BLACK, 16, 0);
    LCD_ShowFloatNum1(170, 5, ADC2data.ADC2_Channel.VBUS_f, 3, 2, GREEN, BLACK, 16);
    LCD_ShowString(220, 5, (uint8_t*)"V", WHITE, BLACK, 16, 0);

    LCD_ShowString(130, 21, (uint8_t*)"Ibus:", WHITE, BLACK, 16, 0);
    LCD_ShowFloatNum1(170, 21, (float)ADC1data.ADC1_Channel.IBUS_f, 3, 2, GREEN, BLACK, 16);
    LCD_ShowString(220, 21, (uint8_t*)"A", WHITE, BLACK, 16, 0);

    LCD_ShowString(130, 37, (uint8_t*)"Tmos:", WHITE, BLACK, 16, 0);
    LCD_ShowFloatNum1(170, 37, (ADC2data.ADC2_Channel.NTC2_f + ADC1data.ADC1_Channel.NTC1_f) / 2.0f, 3, 2, GREEN, BLACK,
                      16);
    LCD_ShowString(220, 37, (uint8_t*)"C", WHITE, BLACK, 16, 0);

    LCD_DrawLine(10, 60, 10, 135, WHITE);
    LCD_DrawLine(230, 60, 230, 135, WHITE);

    LCD_ShowString(20, 40, (uint8_t*)"Mode :", WHITE, BLACK, 16, 0);
    LCD_ShowString(20, 60, (uint8_t*)"Iq.  :", WHITE, BLACK, 16, 0);
    LCD_ShowString(20, 80, (uint8_t*)"Vel. :", WHITE, BLACK, 16, 0);
    LCD_ShowString(20, 100, (uint8_t*)"Pos. :", WHITE, BLACK, 16, 0);
    LCD_ShowString(20, 120, (uint8_t*)"Tor. :", WHITE, BLACK, 16, 0);

    LCD_ShowString(76, 40, (uint8_t*)"[Vel.]", WHITE, BLACK, 16, 0);

    LCD_ShowFloatNum1(68, 60, MATH_ABS_float(0), 3, 2, BRRED, BLACK, 16);
    LCD_ShowString(124, 60, (uint8_t*)"->", WHITE, BLACK, 16, 0);
    LCD_ShowFloatNum1(140, 60, MATH_ABS_float(0), 3, 2, GREEN, BLACK, 16);
    LCD_ShowString(196, 60, (uint8_t*)"A", WHITE, BLACK, 16, 0);

    LCD_ShowFloatNum1(68, 80, MATH_ABS_float(0), 3, 2, GREEN, BLACK, 16);
    LCD_ShowString(124, 80, (uint8_t*)"->", WHITE, BLACK, 16, 0);
    LCD_ShowFloatNum1(140, 80, MATH_ABS_float(0), 3, 2, GREEN, BLACK, 16);
    LCD_ShowString(196, 80, (uint8_t*)"r/s", WHITE, BLACK, 16, 0);

    LCD_ShowFloatNum1(68, 100, MATH_ABS_float(0), 3, 2, GREEN, BLACK, 16);
    LCD_ShowString(124, 100, (uint8_t*)"->", WHITE, BLACK, 16, 0);
    LCD_ShowFloatNum1(140, 100, MATH_ABS_float(0), 3, 2, GREEN, BLACK, 16);
    LCD_ShowString(196, 100, (uint8_t*)"rad", WHITE, BLACK, 16, 0);

    LCD_ShowFloatNum1(68, 120, MATH_ABS_float(0), 3, 2, GREEN, BLACK, 16);
    LCD_ShowString(124, 120, (uint8_t*)"->", WHITE, BLACK, 16, 0);
    LCD_ShowFloatNum1(140, 120, MATH_ABS_float(0), 3, 2, GREEN, BLACK, 16);
    LCD_ShowString(196, 120, (uint8_t*)"N/m", WHITE, BLACK, 16, 0);
}

static void fun_a1()
{
    LCD_ShowString(10, 5, (uint8_t*)"[CHOICE MODE]", GREEN, BLACK, 32, 0);

    LCD_ShowString(20, 60, (uint8_t*)"UseEncoder Ctrl2Mcu", BLACK, WHITE, 16, 0);
    LCD_ShowString(20, 80, (uint8_t*)"Sensorless Ctrl2Mcu", WHITE, BLACK, 16, 0);
    LCD_ShowString(20, 100, (uint8_t*)"Encoder Ctrl2Can", WHITE, BLACK, 16, 0);
    LCD_ShowString(20, 120, (uint8_t*)"Sensorless Ctrl2Can", WHITE, BLACK, 16, 0);
}

static void fun_b1()
{
    LCD_ShowString(10, 5, (uint8_t*)"[CHOICE MODE]", GREEN, BLACK, 32, 0);

    LCD_ShowString(20, 60, (uint8_t*)"UseEncoder Ctrl2Mcu", WHITE, BLACK, 16, 0);
    LCD_ShowString(20, 80, (uint8_t*)"Sensorless Ctrl2Mcu", BLACK, WHITE, 16, 0);
    LCD_ShowString(20, 100, (uint8_t*)"Encoder Ctrl2Can", WHITE, BLACK, 16, 0);
    LCD_ShowString(20, 120, (uint8_t*)"Sensorless Ctrl2Can", WHITE, BLACK, 16, 0);
}

static void fun_c1()
{
    LCD_ShowString(10, 5, (uint8_t*)"[CHOICE MODE]", GREEN, BLACK, 32, 0);

    LCD_ShowString(20, 60, (uint8_t*)"UseEncoder Ctrl2Mcu", WHITE, BLACK, 16, 0);
    LCD_ShowString(20, 80, (uint8_t*)"Sensorless Ctrl2Mcu", WHITE, BLACK, 16, 0);
    LCD_ShowString(20, 100, (uint8_t*)"Encoder Ctrl2Can", BLACK, WHITE, 16, 0);
    LCD_ShowString(20, 120, (uint8_t*)"Sensorless Ctrl2Can", WHITE, BLACK, 16, 0);
}

static void fun_d1()
{
    LCD_ShowString(10, 5, (uint8_t*)"[CHOICE MODE]", GREEN, BLACK, 32, 0);

    LCD_ShowString(20, 60, (uint8_t*)"UseEncoder Ctrl2Mcu", WHITE, BLACK, 16, 0);
    LCD_ShowString(20, 80, (uint8_t*)"Sensorless Ctrl2Mcu", WHITE, BLACK, 16, 0);
    LCD_ShowString(20, 100, (uint8_t*)"Encoder Ctrl2Can", WHITE, BLACK, 16, 0);
    LCD_ShowString(20, 120, (uint8_t*)"Sensorless Ctrl2Can", BLACK, WHITE, 16, 0);
}

static void fun_a21()
{
    LCD_ShowString(10, 5, (uint8_t*)"[CHOICE TARGET]", GREEN, BLACK, 32, 0);

    LCD_ShowString(20, 60, (uint8_t*)"Ctrl POSITION", BLACK, WHITE, 16, 0);
    LCD_ShowString(20, 80, (uint8_t*)"Ctrl SPEED", WHITE, BLACK, 16, 0);
}

static void fun_a22()
{
    LCD_ShowString(10, 5, (uint8_t*)"[CHOICE TARGET]", GREEN, BLACK, 32, 0);

    LCD_ShowString(20, 60, (uint8_t*)"Ctrl POSITION", WHITE, BLACK, 16, 0);
    LCD_ShowString(20, 80, (uint8_t*)"Ctrl SPEED", BLACK, WHITE, 16, 0);
}

static void fun_b21()
{
    LCD_ShowString(10, 5, (uint8_t*)"[CHOICE PROTOCOL]", GREEN, BLACK, 32, 0);

    LCD_ShowString(20, 60, (uint8_t*)"PROTOCOL MIT", BLACK, WHITE, 16, 0);
    LCD_ShowString(20, 80, (uint8_t*)"PROTOCOL POS_SPE", WHITE, BLACK, 16, 0);
    LCD_ShowString(20, 100, (uint8_t*)"PROTOCOL SPE_CUR", WHITE, BLACK, 16, 0);
    LCD_ShowString(20, 120, (uint8_t*)"PROTOCOL CUR_1_4", WHITE, BLACK, 16, 0);
}

static void fun_b22()
{
    LCD_ShowString(10, 5, (uint8_t*)"[CHOICE PROTOCOL]", GREEN, BLACK, 32, 0);

    LCD_ShowString(20, 60, (uint8_t*)"PROTOCOL MIT", WHITE, BLACK, 16, 0);
    LCD_ShowString(20, 80, (uint8_t*)"PROTOCOL POS_SPE", BLACK, WHITE, 16, 0);
    LCD_ShowString(20, 100, (uint8_t*)"PROTOCOL SPE_CUR", WHITE, BLACK, 16, 0);
    LCD_ShowString(20, 120, (uint8_t*)"PROTOCOL CUR_1_4", WHITE, BLACK, 16, 0);
}

static void fun_b23()
{
    LCD_ShowString(10, 5, (uint8_t*)"[CHOICE PROTOCOL]", GREEN, BLACK, 32, 0);

    LCD_ShowString(20, 60, (uint8_t*)"PROTOCOL MIT", WHITE, BLACK, 16, 0);
    LCD_ShowString(20, 80, (uint8_t*)"PROTOCOL POS_SPE", WHITE, BLACK, 16, 0);
    LCD_ShowString(20, 100, (uint8_t*)"PROTOCOL SPE_CUR", BLACK, WHITE, 16, 0);
    LCD_ShowString(20, 120, (uint8_t*)"PROTOCOL CUR_1_4", WHITE, BLACK, 16, 0);
}

static void fun_b24()
{
    LCD_ShowString(10, 5, (uint8_t*)"[CHOICE PROTOCOL]", GREEN, BLACK, 32, 0);

    LCD_ShowString(20, 60, (uint8_t*)"PROTOCOL MIT", WHITE, BLACK, 16, 0);
    LCD_ShowString(20, 80, (uint8_t*)"PROTOCOL POS_SPE", WHITE, BLACK, 16, 0);
    LCD_ShowString(20, 100, (uint8_t*)"PROTOCOL SPE_CUR", WHITE, BLACK, 16, 0);
    LCD_ShowString(20, 120, (uint8_t*)"PROTOCOL CUR_1_4", BLACK, WHITE, 16, 0);
}

static void fun_c21()
{
    LCD_ShowString(10, 5, (uint8_t*)"[CHOICE PROTOCOL]", GREEN, BLACK, 32, 0);

    LCD_ShowString(20, 60, (uint8_t*)"Ctrl SPE_CUR", WHITE, BLACK, 16, 0);
}

static void fun_d21()
{
    LCD_ShowString(10, 5, (uint8_t*)"[CHOICE PROTOCOL]", GREEN, BLACK, 32, 0);

    LCD_ShowString(20, 60, (uint8_t*)"Ctrl SPE_CUR", WHITE, BLACK, 16, 0);
}

static void fun_a31()
{
    UI_LCD.Menu_Parameter.key_value = UI_LCD.table[UI_LCD.Menu_Parameter.key_value].go_back;
    // TODO：输出事件
}

static void fun_a32()
{
    UI_LCD.Menu_Parameter.key_value = UI_LCD.table[UI_LCD.Menu_Parameter.key_value].go_back;
    // TODO：输出事件
}

static void fun_b31()
{
    UI_LCD.Menu_Parameter.key_value = UI_LCD.table[UI_LCD.Menu_Parameter.key_value].go_back;
    // TODO：输出事件
}

static void fun_b32()
{
    UI_LCD.Menu_Parameter.key_value = UI_LCD.table[UI_LCD.Menu_Parameter.key_value].go_back;
    // TODO：输出事件
}

static void fun_b33()
{
    UI_LCD.Menu_Parameter.key_value = UI_LCD.table[UI_LCD.Menu_Parameter.key_value].go_back;
    // TODO：输出事件
}

static void fun_b34()
{
    UI_LCD.Menu_Parameter.key_value = UI_LCD.table[UI_LCD.Menu_Parameter.key_value].go_back;
    // TODO：输出事件
}

static void fun_c31()
{
    UI_LCD.Menu_Parameter.key_value = UI_LCD.table[UI_LCD.Menu_Parameter.key_value].go_back;
    // TODO：输出事件
}

static void fun_d31()
{
    UI_LCD.Menu_Parameter.key_value = UI_LCD.table[UI_LCD.Menu_Parameter.key_value].go_back;
    // TODO：输出事件
}
